package scfg.utils;

import java.io.File;
import java.util.*;

import scfg.output.Display;
import scfg.output.RNAFormattedFile;
import desposito6.math.MyBigDecimal;
import desposito6.math.iDouble;
import desposito6.utils.CommandLine;

public class ConstrainedMFEAppender {
	
	public static boolean enableExtraConstraints = false;
	public static boolean normalGtFold = true;
	
	
	public static enum Prediction {
		GTfold,
		RNAfold
	}
	
	public static boolean skip = false;
	
	public static boolean processFile(File rnaFile, File seqFile, File conFile, File outFile, Prediction predType) {
		if (rnaFile == null || !rnaFile.exists() || !rnaFile.getName().endsWith(".rna"))
			return false;
		try {
			RNAFormattedFile rnaff = new RNAFormattedFile(rnaFile);
//			if(skip && rnaff.get("mfe_gtcon") != null){
//				output.eout("Skipping "+f.getName());
//				return false;
//			}
			String seq = rnaff.get("seq");
			if (seq != null) {
				String sequenceFilename = seqFile.getAbsolutePath();
				String constraintFilename = conFile == null ? null : conFile.getAbsolutePath();
				String outputFilename = outFile.getAbsolutePath();
				String str = null;
				switch (predType) {
				case GTfold :
					str = MfeAquire.getGTfoldDBStructure(sequenceFilename, constraintFilename, outputFilename);
					if (str != null){
						if (normalGtFold)
							rnaff.put("mfe_gt", str);
						else if (enableExtraConstraints)
							rnaff.put("mfe_gtconX", str);
						else
							rnaff.put("mfe_gtcon", str);
						rnaff.writeToFile(false);
					}
					return true;
				case RNAfold : 
					// TODO: 
					if (str != null){
						rnaff.put("mfe_gtcon", str);
						rnaff.writeToFile(false);
					}
					return true;
				default: 
				}
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void processDir(File base) {
		if (base == null || !base.exists() || !base.isDirectory())
			return;
		System.out.println("Opening " + base.getAbsolutePath());
		File[] files = base.listFiles();
		Stack<File> stk = new Stack<File>();
		int count = 0;
		Long start = System.currentTimeMillis();
		for (File f : files) {
			if (f.isDirectory())
				stk.push(f);
			else if (f.getName().endsWith(".rna")) {
				File conFile;
				if (normalGtFold)
					conFile = null;
				else if (enableExtraConstraints)
					conFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/con/").replaceAll(".nopct.rna", ".cnstrX"));
				else
					conFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/con/").replaceAll(".nopct.rna", ".cnstr"));
				File seqFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/sequences/").replaceAll(".nopct.rna", ".seq"));
				File outFile;
				if (normalGtFold)
					outFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/gtfold/").replaceAll(".nopct.rna", ".gt.ct"));
				else if (enableExtraConstraints)
					outFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/gtfold/").replaceAll(".nopct.rna", "._X_.ct"));
				else
					outFile = new File(f.getAbsolutePath().replaceAll("rna_formatted/", "ct_files/gtfold/").replaceAll(".nopct.rna", ".ct"));
				if (!outFile.getParentFile().exists())
					outFile.getParentFile().mkdirs();
				processFile(f,seqFile,conFile,outFile,Prediction.GTfold);
			}
			CommandLine.DisplayBar(files.length, count++, (System
					.currentTimeMillis()
					- start)/1000);
		}
		CommandLine.DisplayBarFinish();
		while (!stk.isEmpty())
			processDir(stk.pop());
		System.out.println(base.getName() + " finished. (" + count
				+ " files processed)");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		normalGtFold = true;
		
		String dirName = "/home/david/Documents/RNA/rna_formatted/5S/";
		File base = new File(dirName);
		enableExtraConstraints = true;
		processDir(base);
//		enableExtraConstraints = false;
//		processDir(base);
		
		dirName = "/home/david/Documents/RNA/rna_formatted/16S/";
		base = new File(dirName);
		enableExtraConstraints = true;
		processDir(base);
//		enableExtraConstraints = false;
//		processDir(base);
		
		System.out.println(Display.box("  DONE!!!  ", "\t"));
	}
}
